//
//  DSBaseViewController.m
//  DaBaiService
//
//  Created by Mac-Mini-KingVision on 16/1/30.
//  Copyright © 2016年 Mac-Mini-KingVision. All rights reserved.
//

#import "DSBaseViewController.h"
#import "DSGrayBorderButton.h"

@interface DSBaseViewController ()

@property (nonatomic, weak) UIView *waitView;
@property (nonatomic, weak) UIImageView *runningImgView;
@property (nonatomic, weak) UIView *errorView;
@property (nonatomic, weak) UILabel *errorLab;
@property (nonatomic, copy) void (^errorTapAction)();
@end

@implementation DSBaseViewController

@synthesize container = _container;


- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GiFHUD dismiss];
}


- (void)showWaitView {
    self.container.hidden = YES;
    self.errorView.hidden = YES;
    if (self.waitView.hidden) {
        self.waitView.hidden = NO;
        self.waitView.alpha = 0.0;
        [UIView animateWithDuration:0.5 animations:^{
            self.waitView.alpha = 1.0;
        }];
    }
    [self.view bringSubviewToFront:self.waitView];
    [self.runningImgView startAnimating];
}

- (void)hideWaitView {
    
    _waitView.hidden = YES;
    _errorView.hidden = YES;
    _container.hidden = NO;
    [self.runningImgView stopAnimating];
    [self.view sendSubviewToBack:_container];
    
}

- (void)hideErrorView {
    _waitView.hidden = YES;
    _errorView.hidden = YES;
    _container.hidden = NO;
    [self.view sendSubviewToBack:_container];
}

- (void)showErrorViewWithMessage:(NSString *)message tapAction:(void (^)())tapAction {
    
    self.waitView.hidden = YES;
    self.container.hidden = YES;
    self.errorView.hidden = NO;
    self.errorTapAction = tapAction;
   // self.errorLab.text = message;
    
    self.errorView.alpha = 0.0;
    [self.view bringSubviewToFront:self.errorView];
    [UIView animateWithDuration:0.5 animations:^{
        self.errorView.alpha = 1.0;
    }];
}


- (UIView *)container {
    if (_container == nil) {
        _container = [[UIView alloc] init];
        _container.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_container];
      //  _container.translatesAutoresizingMaskIntoConstraints = NO;
        [_container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self.view);
            make.top.equalTo(self.view);
        }];
    }
    return _container;
}

- (UIView *)waitView {
    if (_waitView == nil) {
        UIView *waitView = [[UIView alloc] init];
        waitView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:waitView];
        [waitView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.container);
        }];
        
        UIImageView *animateView = [[UIImageView alloc] init];
        animateView.animationImages = @[
                                        [UIImage imageNamed:@"running1"],
                                        [UIImage imageNamed:@"running2"],
                                        [UIImage imageNamed:@"running3"],
                                        [UIImage imageNamed:@"running4"]
                                        ];
        animateView.animationDuration = 0.5;
        animateView.animationRepeatCount = 0;
        [waitView addSubview:animateView];
        [animateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(waitView);
            make.centerY.equalTo(waitView).offset(-50);
            make.width.equalTo(@134);
            make.height.equalTo(@142);
        }];
        _runningImgView = animateView;
        _waitView = waitView;
        
    }
    return _waitView;
}

- (UIView *)errorView {
    
    if (_errorView == nil) {
        UIView *errorView = [UIView new];
        errorView.backgroundColor = [UIColor DSViewControllerBackGroundColor];
        [self.view addSubview:errorView];
        [errorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.container);
        }];
        
        UIImageView *failImgView = [UIImageView new];
        failImgView.image = [UIImage imageNamed:@"netFailIcon"];
        [errorView addSubview:failImgView];
        [failImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(errorView);
            make.centerY.equalTo(errorView).offset(-80);
            make.width.equalTo(@120);
            make.height.equalTo(@120);
        }];
        
        UILabel *label = [UILabel new];
        label.textColor = [UIColor DSSixSixColor];
        label.font = [UIFont systemFontOfSize:17];
        label.text = @"网络请求失败";
        label.textAlignment = NSTextAlignmentCenter;
        [errorView addSubview:label];
        _errorLab = label;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(failImgView.mas_bottom).offset(25);
            make.centerX.equalTo(errorView);
        }];
        
        UILabel *label2 = [UILabel new];
        label2.textColor = [UIColor DSSixSixColor];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"请检查您的网络重新加载吧";
        label2.textAlignment = NSTextAlignmentCenter;
        [errorView addSubview:label2];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label.mas_bottom).offset(10);
            make.centerX.equalTo(errorView);
        }];
        
        DSGrayBorderButton *btn = [DSGrayBorderButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"重新加载" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(errorTap) forControlEvents:UIControlEventTouchUpInside];
        [errorView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label2.mas_bottom).offset(35);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(40);
            make.centerX.equalTo(errorView);
        }];
        
        _errorView = errorView;
    }
    
    return _errorView;
}


- (void)errorTap {
    if (self.errorTapAction) {
        self.errorTapAction();
    }
}

-(void)updateController
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
